#!/bin/bash
# ====================================================================

# 一、初始化
wb="######开始初始化######"
echo -e "\033[32m$wb\033[0m"
# 1、初始化依赖
apt update
apt upgrade -y
apt install unzip -y

# 2、设置路径和密码变量
# root账号的新密码
root_new_password="zl208151"

# chfs账号
chfs_user="zl208151"
# chfs账号
chfs_password="zl208151"
# chfs端口
chfs_port=81

# 图床账号
tuchuang_user="zl208151"
# 图床账号
tuchuang_password="zl208151"
# 图床端口
tuchuang_port=82

# alist账号
alist_user="zl208151"
# alist账号
alist_password="zl208151"
# alist端口
alist_port=83

# sftp账号
sftp_user="zl208151"
# sftp密码
sftp_password="208151"
# sftp端口
sftp_port=84

# 宝塔账号
bt_user="zl208151"
# 宝塔密码
bt_password="zl208151"
# 宝塔端口
bt_port = 85

# mysql密码
mysql_password="zl208151"
# mysql端口
mysql_port=3307

# 本地的ip
host_ip="$(hostname -I | awk '{print $1}')"
# 主文件路径
main_path="/home/zl208151/my_data"
# 预下载文件路径
download_file_path="$main_path/down_files"
# ngrok文件路径
ngrok_path="$download_file_path/ngrok"

# mysql文件路径
mysql_path="$main_path/mysql"
# alist文件路径
alist_path="$main_path/alist"
# 图床文件路径
tuchuang_path="$main_path/tuchuang"
# chfs文件路径
chfs_path="$main_path/chfs"
# sftp文件路径
sftp_path="$main_path/sftp"
# bt文件挂载路径
bt_path="$main_path/bt"
# hoj文件路径
hoj_path="$main_path/hoj"

# 3、创建对应文件夹
mkdir -p $main_path
mkdir -p $ngrok_path
mkdir -p $download_file_path
mkdir -p $mysql_path
mkdir -p $alist_path
mkdir -p $tuchuang_path
mkdir -p $bt_path
mkdir -p $chfs_path

# 4、开启容器变量，0真1假
is_start_chfs=0
is_start_tuchaung=0
is_start_alist=0
is_start_sftp=0
is_start_bt=1
is_start_mysql=0

wb="######依赖已下载完成######"
echo -e "\033[32m$wb\033[0m"

# ====================================================================

# 二、修改root密码
wb="######修改root密码######"
echo -e "\033[32m$wb\033[0m"

printf "%s\n%s" "$root_new_password" "$root_new_password" | sudo passwd root

wb="######root密码修改完成######"
echo -e "\033[32m$wb\033[0m"

# ====================================================================

# 三、开启ssh的root登陆
wb="######开启ssh的root登陆######"
echo -e "\033[32m$wb\033[0m"

# 检查 /etc/ssh/sshd_config 中是否存在 PermitRootLogin yes
SSHD_CONFIG="/etc/ssh/sshd_config"
PERMIT_ROOT_LOGIN="PermitRootLogin yes"
# 检查 PermitRootLogin yes 是否已经存在于 sshd_config 文件中
if grep -q "$PERMIT_ROOT_LOGIN" "$SSHD_CONFIG"; then
    wb="######PermitRootLogin yes 已存在于 $SSHD_CONFIG. 跳过######"
    echo -e "\033[34m$wb\033[0m"
else
    # 如果不存在，则在第34行添加 PermitRootLogin yes
    wb="Adding PermitRootLogin yes to $SSHD_CONFIG on line 34."
    echo -e "\033[31m$wb\033[0m"
    sed -i '34i '"$PERMIT_ROOT_LOGIN" "$SSHD_CONFIG"
fi

wb="######开启ssh的root登陆已开启######"
echo -e "\033[32m$wb\033[0m"

# ====================================================================

# 四、安装docker和docker_compose
wb="######正在安装docker######"
echo -e "\033[32m$wb\033[0m"

sudo apt install docker.io -y

wb="######docker已安装完成######"
echo -e "\033[32m$wb\033[0m"

wb="######正在安装docker_compose######"
echo -e "\033[32m$wb\033[0m"

sudo apt install docker-compose -y

wb="######docker_compose已安装完成######"
echo -e "\033[32m$wb\033[0m"

# ====================================================================

# 五、更换docker国内源
wb="######正在更换docker国内源######"
echo -e "\033[32m$wb\033[0m"
# 创建文件
sudo mkdir -p /etc/docker
# 写入数据
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://docker.1ms.run",
    "https://proxy.1panel.live",
    "https://docker.ketches.cn"
  ]
}
EOF
# 重启服务
sudo systemctl daemon-reload
systemctl restart docker

wb="######更换docker国内源已完成######"
echo -e "\033[32m$wb\033[0m"

# ====================================================================

# 六、下载指定软件
wb="######正在下载指定软件######"
echo -e "\033[32m$wb\033[0m"
# 下载ngrok
# https://www.ngrok.cc/sunny/linux_amd64.zip?v=2.1
wget -P $ngrok_path -O "$ngrok_path/linux_amd64.zip" https://www.ngrok.cc/sunny/linux_amd64.zip?v=2.1
if [ $? -ne 0 ]; then
    wb="下载ngrok失败，请检查网络或URL"
    echo -e "\033[34m$wb\033[0m"
    exit 1
fi
unzip "$ngrok_path/linux_amd64.zip" -d "$ngrok_path/"
mv "$ngrok_path/linux_amd64/sunny" "$ngrok_path/sunny"
rm -rf "$ngrok_path/linux_amd64" "$ngrok_path/linux_amd64.zip"
chmod +x "$ngrok_path/sunny"

wb="######ngrok已下载完成######"
echo -e "\033[32m$wb\033[0m"

# 拉取镜像
docker pull pch18/baota:lnp
docker pull natfrp.com/frpc:latest
docker pull openfrp/frpc:latest
docker pull starryfrp/frpc:22.5.13
docker pull xhofe/alist:latest
docker pull mysql:8.0
docker pull registry.cn-chengdu.aliyuncs.com/7104475/chfs:v3.1
docker pull ddsderek/easyimage:latest

wb="######下载指定软件已完成######"
echo -e "\033[32m$wb\033[0m"

# ====================================================================

# 七、开启指定docker容器
wb="######正在开启指定docker容器######"
echo -e "\033[32m$wb\033[0m"

# 判断chfs是否开启
# docker run --name chfs-docker  --restart=always  -d -it -p "$chfs_port":8080 -e  user_name="$chfs_user" -e user_password="$chfs_password" -v "$chfs_path/chfs_conf:/config" -v "$chfs_path/chfs_data:/data" registry.cn-chengdu.aliyuncs.com/7104475/chfs:v3.1

# 判断图床是否开启
# docker run -d --name tuchuang-docker --restart=always -p "$tuchuang_port":80 -e TZ=Asia/Shanghai -e PUID=1000 -e PGID=1000 -e DEBUG=false -v "$tuchuang_path/config:/app/web/config" -v "$tuchuang_path/i:/app/web/i" ddsderek/easyimage:latest

# 判断alist是否开启
# docker run -d --name=alist-docker  --restart=always -v "$alist_path:/opt/alist/data" -p "$alist_port":5244 -e PUID=0 -e PGID=0 -e UMASK=022 xhofe/alist:latest
#设置密码
# docker exec -it alist-docker ./alist admin set "$alist_password"

# 判断sftp是否开启
# docker run -d --name sftp_docker --restart=always -p "$sftp_port":22 -v "$sftp_path":/home/admin/disk_data atmoz/sftp "$sftp_user":"$sftp_password":1001

# 判断mysql是否开启
# docker run -d --name mysql-docker --restart=always -p "$mysql_port":3306 -v "$mysql_path:/var/lib/mysql" -e MYSQL_ROOT_PASSWORD="$mysql_password" mysql:8.0

# 判断bt是否开启
docker run -d --name bt_docker --restart=always --network=host --privileged=true --shm-size=1g  -v "$bt_path/wwwroot":/www/wwwroot pch18/baota:lnp
expect << 'EOF'
# 设置超时时间
set timeout 10

# 启动容器内的 bash 会话
spawn docker exec -it bt_docker /bin/bash

# 等待合适时机输入内容
expect "$ " { send "bt 8\r" }
expect "$ " { send "85\r" }
expect "$ " { send "bt 6\r" }
expect "$ " { send "zl208151\r" }
expect "$ " { send "bt 5\r" }
expect "$ " { send "zl208151\r" }
set timeout 5
# 发送退出命令
expect "$ " { send "exit\r" }

# 等待会话结束
expect eof
EOF


wb="######开启指定docker容器已完成######"
echo -e "\033[32m$wb\033[0m"

# ====================================================================

# 八、配置网页
wb="######正在配置网页######"
echo -e "\033[32m$wb\033[0m"

wb="######配置网页已完成######"
echo -e "\033[32m$wb\033[0m"

# ====================================================================

wb="######一键配置已完成######"
echo -e "\033[32m$wb\033[0m"
